﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp6
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入1 2 3 来进入不同功能  1订单管理系统演示  2随机数字处理  3反射创建Person实例 ");
            var a = Console.ReadLine();
            switch (a)
            {
                case "1":
                    Console.WriteLine("===== 订单管理系统演示 =====");
                    OrderManagementDemo();
                    break;
                case "2":
                    Console.WriteLine("\n===== 随机数字处理 =====");
                    RandomNumberProcessing();
                    break;
                case "3":
                    Console.WriteLine("\n===== 反射创建Person实例 =====");
                    CreatePersonWithReflection();
                    break;
            }
            Console.ReadLine();

        }
        static void OrderManagementDemo()
        {
            OrderService service = new OrderService();

            // 添加一些测试数据
            var customer1 = new Customer { Id = "C001", Name = "张三" };
            var customer2 = new Customer { Id = "C002", Name = "李四" };
            var product1 = new Product { Id = "P001", Name = "笔记本电脑", Price = 5999 };
            var product2 = new Product { Id = "P002", Name = "手机", Price = 3999 };
            var product3 = new Product { Id = "P003", Name = "耳机", Price = 299 };

            try
            {
                // 添加订单1
                var order1 = new Order { OrderId = "O001", Customer = customer1, OrderDate = DateTime.Now };
                order1.AddDetail(new OrderDetail { Product = product1, Quantity = 1 });
                order1.AddDetail(new OrderDetail { Product = product3, Quantity = 2 });
                service.AddOrder(order1);

                // 添加订单2
                var order2 = new Order { OrderId = "O002", Customer = customer2, OrderDate = DateTime.Now };
                order2.AddDetail(new OrderDetail { Product = product2, Quantity = 1 });
                service.AddOrder(order2);

                // 添加订单3
                var order3 = new Order { OrderId = "O003", Customer = customer1, OrderDate = DateTime.Now };
                order3.AddDetail(new OrderDetail { Product = product1, Quantity = 1 });
                order3.AddDetail(new OrderDetail { Product = product2, Quantity = 1 });
                service.AddOrder(order3);
            }
            catch (Exception e)
            {
                Console.WriteLine($"添加订单出错: {e.Message}");
            }

            // 查询
            Console.WriteLine("所有订单:");
            service.GetAllOrders().ForEach(Console.WriteLine);

            Console.WriteLine("\n按客户'张'查询:");
            service.QueryByCustomer("张").ForEach(Console.WriteLine);

            Console.WriteLine("\n按商品'手机'查询:");
            service.QueryByProductName("手机").ForEach(Console.WriteLine);

            Console.WriteLine("\n按金额范围3000-6000查询:");
            service.QueryByAmountRange(3000, 6000).ForEach(Console.WriteLine);

            // 修改订单
            try
            {
                var orderToUpdate = new Order { OrderId = "O001", Customer = customer1, OrderDate = DateTime.Now };
                orderToUpdate.AddDetail(new OrderDetail { Product = product1, Quantity = 2 }); // 修改数量
                service.UpdateOrder(orderToUpdate);
                Console.WriteLine("\n修改后的订单O001:");
                Console.WriteLine(service.QueryByOrderId("O001").First());
            }
            catch (Exception e)
            {
                Console.WriteLine($"修改订单出错: {e.Message}");
            }

            // 删除订单
            try
            {
                service.RemoveOrder("O002");
                Console.WriteLine("\n删除O002后的所有订单:");
                service.GetAllOrders().ForEach(Console.WriteLine);
            }
            catch (Exception e)
            {
                Console.WriteLine($"删除订单出错: {e.Message}");
            }

            // 自定义排序
            Console.WriteLine("\n按总金额排序:");
            service.Sort((x, y) => y.TotalAmount.CompareTo(x.TotalAmount));
            service.GetAllOrders().ForEach(Console.WriteLine);
            //第七次作业 g1
            try
            {
                // 导出订单
                string exportPath = "orders.xml";
                service.Export(exportPath);

                // 导入订单
                OrderService newService = new OrderService();
                newService.Import(exportPath);

                Console.WriteLine("\n导入后的订单:");
                newService.GetAllOrders().ForEach(Console.WriteLine);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"XML操作出错: {ex.Message}");
            }
        }
        static void RandomNumberProcessing()
        {
            Random random = new Random();
            var numbers = Enumerable.Range(0, 100)
                                   .Select(_ => random.Next(0, 1001))
                                   .ToList();

            // 从大到小排序
            var sortedNumbers = numbers.OrderByDescending(n => n).ToList();

            // 求和与平均数
            double sum = numbers.Sum();
            double average = numbers.Average();

            Console.WriteLine("排序后的数字:");
            Console.WriteLine(string.Join(", ", sortedNumbers.Take(10)) + "..."); // 只显示前10个

            Console.WriteLine($"\n总和: {sum}");
            Console.WriteLine($"平均数: {average:F2}");
        }
        static void CreatePersonWithReflection()
        {
            // 获取Person类型
            Type personType = typeof(Person);

            // 获取带参数的构造函数
            var constructor = personType.GetConstructor(new[] { typeof(string), typeof(int) });

            // 使用反射创建实例
            if (constructor != null)
            {
                object person = constructor.Invoke(new object[] { "张三", 25 });
                Console.WriteLine("使用反射创建的Person对象:");
                Console.WriteLine(person);
            }
        }
    }
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Person() { }

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public override string ToString()
        {
            return $"Person[Name={Name}, Age={Age}]";
        }
    }
    // 商品类
    public class Product
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }

        public override string ToString()
        {
            return $"商品[ID:{Id}, 名称:{Name}, 价格:{Price:C}]";
        }
    }

    // 客户类
    public class Customer
    {
        public string Id { get; set; }
        public string Name { get; set; }

        public override string ToString()
        {
            return $"客户[ID:{Id}, 姓名:{Name}]";
        }
    }

    // 订单明细类
    public class OrderDetail
    {
        public Product Product { get; set; }
        public int Quantity { get; set; }

        public double Amount => Product.Price * Quantity;

        public override bool Equals(object obj)
        {
            return obj is OrderDetail detail &&
                   EqualityComparer<Product>.Default.Equals(Product, detail.Product);
        }

        public override int GetHashCode()
        {
            return Product?.GetHashCode() ?? 0;
        }

        public override string ToString()
        {
            return $"{Product} x{Quantity} = {Amount:C}";
        }
    }

    // 订单类
    public class Order : IComparable<Order>
    {
        public string OrderId { get; set; }
        public Customer Customer { get; set; }
        public DateTime OrderDate { get; set; }
        public List<OrderDetail> Details { get; } = new List<OrderDetail>();

        public double TotalAmount => Details.Sum(d => d.Amount);

        public void AddDetail(OrderDetail detail)
        {
            if (Details.Contains(detail))
                throw new ArgumentException("订单明细已存在");
            Details.Add(detail);
        }

        public override bool Equals(object obj)
        {
            return obj is Order order &&
                   OrderId == order.OrderId;
        }

        public override int GetHashCode()
        {
            return OrderId?.GetHashCode() ?? 0;
        }

        public int CompareTo(Order other)
        {
            if (other == null) return 1;
            return OrderId.CompareTo(other.OrderId);
        }

        public override string ToString()
        {
            return $"订单[编号:{OrderId}, 客户:{Customer}, 日期:{OrderDate:yyyy-MM-dd}, 总金额:{TotalAmount:C}]\n" +
                   string.Join("\n", Details.Select(d => "  " + d));
        }
    }
}
